package com.dlwx.parttimetalent.views;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.FilterListAdapter;
import com.dlwx.parttimetalent.bean.OrderRecBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.inter.DialogFilerInter;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.flyco.dialog.widget.base.BaseDialog;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.reflect.TypeToken;
import com.jaygoo.widget.RangeSeekBar;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/23.
 */

public class FilterDialog extends BaseDialog {

    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    @BindView(R.id.rb_men)
    RadioButton rbMen;
    @BindView(R.id.rb_women)
    RadioButton rbWomen;
    @BindView(R.id.tv_range)
    TextView tvRange;
    @BindView(R.id.seekbar)
    RangeSeekBar seekbar;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.rb_all)
    RadioButton rbAll;
    @BindView(R.id.tv_distance)
    TextView tvDistance;
    @BindView(R.id.rg_time)
    RadioGroup rgTime;
    private Context context;
    private FilterListAdapter mAdapter;
    private String chooseId;
    private String sex;
    private String minage;
    private String maxage;
    private String distance = "";
    private String labelclassid = "";
    private DialogFilerInter inter;
    private String filterTime;


    public FilterDialog(Context context, DialogFilerInter inter) {
        super(context);
        this.context = context;
        this.inter = inter;
    }

    @Override
    public View onCreateView() {
        widthScale(0.92f);
//        showAnim(new Swing());
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_filter, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void setUiBeforShow() {
        float[] ff = seekbar.getCurrentRange();
        tvRange.setText("全部");
        seekbar.setValue(0, 100);
        seekbar.setOnRangeChangedListener(new RangeSeekBar.OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar view, float min, float max, boolean isFromUser) {
                minage = min + "";
                maxage = max + "";
                tvRange.setText((int) min + "-" + (int) max);
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        mAdapter = new FilterListAdapter(context);
        mAdapter.setDataList(new ArrayList<OrderRecBean.BodyBean>());
        LRecyclerViewAdapter lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        //divider
        DividerDecoration divider = new DividerDecoration.Builder(context)
                .setHeight(R.dimen.size_1)
//                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.bg)
                .build();
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        recyclerView.setLoadMoreEnabled(false);
        recyclerView.setPullRefreshEnabled(false);

        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                List<OrderRecBean.BodyBean.ChildBeanX> beans = mAdapter.getDataList().get(position).getChild();
                labelclassid = mAdapter.getDataList().get(position).getLabelclassid() + "";
                if (mAdapter.getDataList().get(position).getLabelclassid() == 15) {
                    DialogFilterHyyr dialog = new DialogFilterHyyr(context, beans, new DialogFilerInter() {
                        @Override
                        public void callBack(String datas, String labeid, String sex, String minage, String maxage, String distance,String filterTime) {
                            chooseId = datas;
                        }
                    });
                    dialog.show();

                } else {
                    ClassifyDialog dialog = new ClassifyDialog(context, beans, new DialogFilerInter() {
                        @Override
                        public void callBack(String datas, String labeid, String sex, String minage, String maxage, String distance,String filterTime) {
                            chooseId = datas;
                        }

                    });
                    dialog.show();

                }
            }
        });
        rgTime.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                LogUtils.i(group.getCheckedRadioButtonId()+"---"+checkedId);
                switch (group.getCheckedRadioButtonId()){
                    case R.id.rb_1:
                        filterTime="1";
                        break;
                    case R.id.rb_2:
                        filterTime="2";
                        break;
                    case R.id.rb_3:
                        filterTime="3";
                        break;
                    case R.id.rb_4:
                        filterTime="4";
                        break;
                }
            }
        });
        getDatas();
    }

    @OnClick({R.id.rb_men, R.id.rb_women, R.id.tv_cancel, R.id.tv_confirm, R.id.rb_all, R.id.tv_distance})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rb_men:
                sex = "1";
                rbMen.setChecked(true);
                rbWomen.setChecked(false);
                rbAll.setChecked(false);
                break;
            case R.id.rb_women:
                sex = "2";
                rbMen.setChecked(false);
                rbAll.setChecked(false);
                rbWomen.setChecked(true);
                break;
            case R.id.rb_all:
                sex = "";
                rbMen.setChecked(false);
                rbWomen.setChecked(false);
                rbAll.setChecked(true);
                break;
            case R.id.tv_distance:
                if (TextUtils.isEmpty(distance)) {
                    tvDistance.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
                    distance = "1";
                } else {
                    tvDistance.setTextColor(ContextCompat.getColor(context, R.color.black));
                    distance = "";
                }
                break;
            case R.id.tv_cancel:
                this.dismiss();
                break;
            case R.id.tv_confirm:
                inter.callBack(labelclassid, chooseId, sex, minage, maxage, distance,filterTime);
                this.dismiss();
                break;
        }
    }

    private void getDatas() {
        OkgoUtil.get(Http.GetIntentList, context)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        OrderRecBean parentBean = DealResult.getInstance().dealDatas(context, response, new TypeToken<OrderRecBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            mAdapter.addAll(parentBean.getBody());
                            recyclerView.refreshComplete(mAdapter.getItemCount());
                        } else {
                            ToastUtils.toastForShort(context, parentBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }
                });
    }
}
